package com.tianruan.dc.modules.sys.user.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tianruan.dc.common.utils.IdGen;
import com.tianruan.dc.modules.sys.user.entity.Result ;
import com.tianruan.dc.modules.sys.user.entity.Work ;
import com.tianruan.dc.modules.sys.user.entity.WorkInfo;
import com.tianruan.dc.modules.sys.user.mapper.WorkMapper ;
import com.tianruan.dc.modules.sys.user.service.WorkService ;
import com.tianruan.dc.modules.sys.user.utils.PageBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Author: Vivienne
 */
@Service
@Slf4j

public class WorkServiceImpl extends ServiceImpl<WorkMapper, Work> implements WorkService {
    @Autowired
    private WorkMapper workMapper;
    @Override
    public Work insert(String username, String tile) {
        Work work = null;
        String workId = null;
        String id = null;
        if (tile != null) {
            id = IdGen.uuid();
            workId = RandomUtil.randomNumbers(6);
            work = createWork(id, username, tile, workId);
            if (work != null) {
                work.setWorkId(workId);
            } else {
                // 如果 createWork 返回了 null，表示创建作文失败
                Result.fail("创建作文失败");
            }
        } else {
            Result.fail("作文信息设置错误");
        }
        return work;
    }
    private Work createWork(String id,String username,String tile,String workId) {
        Work work = new Work();
        work.setId(id);
        work.setUsername(username);
        work.setTile(tile);
        work.setWorkId(workId);
        //保存work
        save(work);
        return work;
    }

    @Override
    public Work getByWorkId(String workId) {
        Work work = query().eq("work_id", workId).one();
        return work;
    }

    @Override
    public PageBean<Work> PageWork(Integer page, Integer pageSize, String workId) {
        //创建pageBean对象
        PageBean<Work> pageBean = new PageBean<>();
        //开启分页查询  和 模糊查询
        PageHelper.startPage(page,pageSize);
        List<Work> pageInfo = workMapper.pageWork(page,pageSize,workId);
        Page<Work> page1 = (Page<Work>) pageInfo;
        //把数据填充到pagebean对象中
        pageBean.setTotal((long) page1.size());
        pageBean.setItems(page1.getResult());
        //返回pagebean对象
        return pageBean;

    }

    @Override
    public PageBean<WorkInfo> PageWorkInfo(Integer page, Integer pageSize, String workId) {
        //创建pageBean对象
        PageBean<WorkInfo> pageBean = new PageBean<>();
        //开启分页查询  和 模糊查询
        PageHelper.startPage(page,pageSize);
        List<WorkInfo> pageInfo = workMapper.pageWorkInfo(page,pageSize,workId);
        Page<WorkInfo> page1 = (Page<WorkInfo>) pageInfo;
        //把数据填充到pagebean对象中
        pageBean.setTotal((long) page1.size());
        pageBean.setItems(page1.getResult());
        //返回pagebean对象
        return pageBean;
    }


}
